<?php

namespace app\admin\controller;

use app\common\model\Article;
use think\Request;
use think\facade\Db;
use app\common\model\AuthRule;
use app\common\model\User;


class ArticleController extends BaseController
{

    /**
     * 文章列表
     * @params string
     * @return
     */
    public function list(Request $request)
    {
        if($this->method == 'POST')
        {
            $where = $request->post();
            $whereOr = [];
            $search = [];
            if(!empty($where['title']))
            {
                $search[] = ['title', 'like', '%'.$where['title'].'%'];
            }
            if(!empty($where['tag_id']))
            {
                $search['tag_id'] = $where['tag_id'];
            }
            if(!empty($where['category_id']))
            {
                $search['category_id'] = $where['category_id'];
            }
            if(is_numeric($where['status']))
            {
                $search['a.status'] = $where['status'];
            }
            $data = Article::getArticle($search, $whereOr,'a.id desc', $this->page, $this->limit);
            $total = $data['total'];
            $list  = $data['list'];

            return returnData($list, $total);
        }else{
            $tag = Article::getSelectTag();
            return view('article/article_list',['tag'=>$tag]);
        }

    }

    /**
     * 添加
     * @params string
     * @return
     */
    public function add(Request $request)
    {
        if($this->method == 'POST')
        {
            $post_data = $request->post('form_data');
            if(!empty($form_data))
            {
                return returnFail('提交内容不能为空');
            }
            $rich_text = $request->post('rich_text');
            parse_str($post_data, $form_data);

            $form_data = array_merge($form_data, ['content'=>$rich_text]);
            if(empty($form_data['title']))
            {
                return returnFail('请填入文章标题');
            }
            $res = Article::create($form_data);
            if($res)
            {
                return returnSuccess('添加成功');
            }
            return returnFail('添加失败');
        }else{
            $tag = Article::getSelectTag();
            return view('article/add_article',['tag'=>$tag]);
        }
    }

    /**
     * 修改
     * @params string
     * @return
     */
    public function edit(Request $request)
    {
        if($this->method == 'POST')
        {
            $post_data = $request->post('form_data');
            if(!empty($form_data))
            {
                return returnFail('提交内容不能为空');
            }
            $rich_text = $request->post('rich_text');
            parse_str($post_data, $form_data);

            $form_data = array_merge($form_data, ['content'=>$rich_text]);
            if(empty($form_data['title']))
            {
                return returnFail('请填入文章标题');
            }
            $res = Article::update($form_data, ['id'=>$form_data['id']]);
            if($res)
            {
                return returnSuccess('修改成功');
            }
            return returnFail('修改失败');
        }else{
            $assign['info'] = Article::find($request->get('id'));
            $assign['tag']  = Article::getSelectTag();

            return view('article/edit_article', $assign);
        }
    }

    /**
     * 删除
     * @params string
     * @return
     */
    public function del(Request $request)
    {
        if($this->method == 'POST')
        {
            $id = $request->post('id');

            Article::destroy($id);   //真删除

            return returnSuccess('删除成功');
        }
        return returnFail('非法请求');
    }

    /**
     * 文章标签
     * @params string
     * @return
     */
    public function tag(Request $request)
    {
        if($this->method == 'POST')
        {
            $list = Article::getTagTree();

            return returnData($list, 3);
        }else{
            return view('article/tag_list');
        }
    }

    /**
     * 添加标签
     * @return
     */
    public function addTag(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            if(empty($data['name']))
            {
                return returnFail('请填入标签名称');
            }
            if(Db::name('tag')->where('name',$data['name'])->find())
            {
                return returnFail('标签名称已存在');
            }
            $data['create_time'] = getNowTime();
            $res = Db::name('tag')->strict(false)->insert($data);
            if($res)
            {
                return returnSuccess('添加成功');
            }
            return returnFail('添加失败');
        }else{

            //获取一级标签
            $tag = Db::name('tag')->where(['status'=>1,'pid'=>0])->select();

            return view('article/add_tag',['list'=>$tag]);
        }
    }


    public function editTag(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            if(empty($data['name']))
            {
                return returnFail('请填入标签名称');
            }
            if(Db::name('tag')->where('id','<>',$data['id'])->where('name',$data['name'])->find())
            {
                return returnFail('标签名称已存在');
            }
            $data['create_time'] = getNowTime();
            $res = Db::name('tag')->strict(false)->where('id', $data['id'])->update($data);
            if($res)
            {
                return returnSuccess('修改成功');
            }
            return returnFail('修改失败');
        }else{
            //获取一级标签
            $tag  = Db::name('tag')->where(['status'=>1,'pid'=>0])->select();
            $info = Db::name('tag')->where('id', input('id'))->find();
            return view('article/edit_tag',['list'=>$tag, 'info'=>$info]);
        }
    }

    //删除标签
    public function delTag(Request $request)
    {
        if($this->method == 'POST')
        {
            $id = $request->post('id');

            Db::name('tag')->whereIn('id', $id)->delete();

            return returnSuccess('删除成功');
        }
        return returnFail('非法请求');
    }


}
